package cn.com.myshiro.controller;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import Test.tools.Page;
import java.util.Map;
import java.util.HashMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.myshiro.bean.Sysuser;
import cn.com.myshiro.service.SysuserService;

@Controller
public class SysuserController {

      @Autowired
      SysuserService service;

      @RequestMapping(value = "/sysuserView.do")
      public String sysuserView(ModelMap modelMap,String pageNo, String choice, HttpSession session){
			int currentPage = 1;
			if (pageNo != null) {
				currentPage = Integer.parseInt(pageNo);
			}
			int countNum = service.countSysuser();
			Page _page = new Page(5, countNum, currentPage);
			_page.setTotalRecord(countNum);
			_page.setPageNumStart(_page.getPageNumStart());
			_page.setPageNumEnd(_page.getPageNumEnd());
			_page.setCurrentPage(currentPage);
			Map<Object,Object> map = new HashMap<Object,Object>();
			map.put("nowpage", _page.getStart());
			map.put("pagesize", _page.getPageRecord());
			List<Sysuser> sysuserList = null;
			try {
				sysuserList = service.allSysuser(map);
			} catch (Exception e) {
				e.printStackTrace();
			}
			modelMap.put("sysuserList", sysuserList);
			modelMap.put("showPage", _page);
          return "back/jsp/sysuser/sysuserView";
     }

      @RequestMapping(value = "/sysuserDel{id}.do")
      public String sysuserDel(@PathVariable("id")int id ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             service.delSysuser(id);
             String message1="删除成功";
             String message2="请返回……";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
            return "redirect:/sysuserView.do";
      }

      @RequestMapping(value ="/sysuserGoAdd.do")
             public String sysuserGoAdd(ModelMap modelMap,String pageNo, String choice, HttpSession session){
             return "back/jsp/sysuser/sysuserAdd";
      }

      @RequestMapping(value = "/sysuserAdd.do",method=RequestMethod.POST)
      public String sysuserAdd(Sysuser form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             service.addSysuser(form);
             String message1="添加成功";
             String message2="请返回……";
             String  url="sysuserView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
             modelMap.put("url", url);
             return "redirect:/sysuserView.do";
      }

      @RequestMapping(value = "/sysuserGoUpdate{id}.do")
      public String sysuserGoUpdate(@PathVariable("id")int id ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             Sysuser sysuser = service.sysuser(id);
             modelMap.put("sysuser", sysuser);
             return "back/jsp/sysuser/sysuserUpdate";
      }

      @RequestMapping(value = "/sysuserUpdate.do",method=RequestMethod.POST)
      public String sysuserUpdate(Sysuser form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             service.updateSysuser(form);
             String message1="修改成功";
             String message2="请返回……";
             String  url="sysuserView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
             modelMap.put("url", url);
             return "redirect:/sysuserView.do";
      }

 }
